package fnord.jovian.tango;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TheStat implements TheObject {

	private Rectangle2D.Double _barBack, _barFront;
	private Color _barColor = Color.gray, _frontColor = Color.darkGray;
	private double _length, _val = 0, _titleWidth = 100,
	_textWidth = 15, _barHeight = 10;
	private String _title;
	private boolean _textBased;


	public TheStat(String title, double length, double w, boolean textBased) {
		_textBased = textBased;
		_title = title + ": ";
		if (!_textBased) {
			_title = title;
			_length = length;
			_barFront = new Rectangle2D.Double(0,0,0,_barHeight);
			_barBack = new Rectangle2D.Double(0,0,w - _titleWidth - _textWidth,_barHeight);
		}
		else {
			_val = length;
			_barFront = new Rectangle2D.Double(0,0,0,_barHeight);
			_barBack = new Rectangle2D.Double(0,0,w - _titleWidth - _textWidth,_barHeight);
		}

	}

	@Override
	public void paint(Graphics2D g) {
		g.setFont(new Font("Arial", Font.BOLD, (int)Math.round(12.0 * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0)));
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		FontMetrics fm = g.getFontMetrics();
		int fontHeight = fm.getAscent() + fm.getDescent();
		int fontWidth = fm.stringWidth(_title);
		if (_textBased) {
			g.setColor(Color.WHITE);
			g.drawString(_title, (int)(_barBack.getX()- _titleWidth), (int)(_barBack.getY() + fm.getAscent() + (_barBack.getHeight() - fontHeight)/2.0));
			g.setFont(new Font("Arial", Font.PLAIN, (int)Math.round(10.0 * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0)));
			g.drawString(String.valueOf((int)_val), (int)(_barBack.getX() - _titleWidth + fontWidth), (int)(_barBack.getY() + fm.getAscent() + (_barBack.getHeight() - fontHeight)/2.0));
		}
		else {
			g.setColor(_barColor);
			g.fill(_barBack);
			g.setColor(_frontColor);
			g.fill(_barFront);
			g.setColor(Color.WHITE);
			int val = (int)(Math.round(_length*(_barFront.getWidth())/_barBack.getWidth()));
			g.drawString(_title, (int)(_barBack.getX()- _titleWidth), (int)(_barBack.getY() + fm.getAscent() + (_barBack.getHeight() - fontHeight)/2.0));
			g.drawString(String.valueOf(val), (int)(_barBack.getX() + _barBack.getWidth() + 5), (int)(_barBack.getY() + fm.getAscent() + (_barBack.getHeight() - fontHeight)/2.0));
		}
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	@Override
	public boolean contains(Point p) {
		return true;
	}

	@Override
	public boolean containsClick(Point p) {
		return true;
	}

	@Override
	public boolean containsDrag(Point p) {
		return true;
	}

	public double getValue() {
		return (Math.round(_length*(_barFront.getWidth())/_barBack.getWidth()));
	}

	@Override
	public boolean containsUp(Point p) {
		return true;
	}

	@Override
	public double getX() {
		return (_barBack.getX() - _titleWidth);
	}

	@Override
	public double getY() {
		return _barBack.getY();
	}

	@Override
	public double getCenterX() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double getCenterY() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLocation(double x, double y) {
		_barBack.setFrame(x + _titleWidth, y, _barBack.getWidth(), _barBack.getHeight());
		_barFront.setFrame(x + _titleWidth, y, _barFront.getWidth(), _barFront.getHeight());
	}

	@Override
	public double getWidth() {
		return (_barBack.getWidth() + _textWidth + _titleWidth);
	}

	@Override
	public double getHeight() {
		return (_barBack.getHeight());
	}

	public void setLevel (double l) {
		double w = Math.max(Math.min(_barBack.getWidth()*(l/_length), _barBack.getWidth()), 0);
		_barFront.setFrame(_barFront.getX(), _barFront.getY(), w, _barFront.getHeight()); 
	}

	@Override
	public void setSize(double w, double h) {
		// TODO Auto-generated method stub

	}

}
